#!/bin/bash
JROOT=/usr/etc/jkzuc
DEFCFG=$JROOT/configs/default
USRCFG=$JROOT/configs/JAKA
USRSCRIPT=$JROOT/scripts
SETCAP=/usr/sbin/setcap
PATCHELF=$JROOT/bin/patchelf
NETCFG_DIR=/etc/systemd/network
SYSCTL=systemctl

__intall_file(){
    if [ -f $2 ];then
        return
    fi 
    install -m $3 $1 $2
}

# 通过网络接口名确认工控机类型
install_network(){    
    # cp -n 无文件则复制，否则跳过
    __intall_file $DEFCFG/LAN1.network $USRCFG/LAN1.network 666
    __intall_file $DEFCFG/LAN2.network $USRCFG/LAN2.network 666
    __intall_file $DEFCFG/LAN3.network $USRCFG/LAN3.network 666
    __intall_file $DEFCFG/LAN4.network $USRCFG/LAN4.network 666
    __intall_file $DEFCFG/LAN5.network $USRCFG/LAN5.network 666
    # 通过网卡名判断工控机类型
    if [ "$(ip link show | grep -oP "enp1s0:|enp2s0:|enp3s0:|enp4s0:|enp5s0:" | wc -l)" -eq 5 ];then 
    # RoboHub
        sed -i -e 's/^Name=.*/Name=enp4s0/g' $USRCFG/LAN1.network 
        sed -i -e 's/^Name=.*/Name=enp3s0/g' $USRCFG/LAN2.network 
        sed -i -e 's/^Name=.*/Name=enp2s0/g' $USRCFG/LAN3.network 
        sed -i -e 's/^Name=.*/Name=enp1s0/g' $USRCFG/LAN4.network 
        sed -i -e 's/^Name=.*/Name=enp5s0/g' $USRCFG/LAN5.network 
    elif  [ "$(ip link show | grep -oP "enp1s0:|enp2s0:|enp3s0:|enp4s0:" | wc -l)" -eq 4 ];then 
    # cabv3
        sed -i -e 's/^Name=.*/Name=enp1s0/g' $USRCFG/LAN1.network 
        sed -i -e 's/^Name=.*/Name=enp2s0/g' $USRCFG/LAN2.network 
        sed -i -e 's/^Name=.*/Name=enp3s0/g' $USRCFG/LAN3.network 
        sed -i -e 's/^Name=.*/Name=enp4s0/g' $USRCFG/LAN4.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN5.network 
    elif [ "$(ip link show | grep -oP "enp2s0:|enp4s0:" | wc -l)" -eq 2 ];then   
        sed -i -e 's/^Name=.*/Name=enp2s0/g' $USRCFG/LAN1.network 
        sed -i -e 's/^Name=.*/Name=enp4s0/g' $USRCFG/LAN2.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN3.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN4.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN5.network 
    elif [ "$(ip link show | grep -oP "enp2s0:|enp3s0:" | wc -l)" -eq 2 ];then  
    # cabv2.1
        sed -i -e 's/^Name=.*/Name=enp3s0/g' $USRCFG/LAN1.network 
        sed -i -e 's/^Name=.*/Name=enp2s0/g' $USRCFG/LAN2.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN3.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN4.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN5.network 
    elif [ "$(ip link show | grep -oP "enp1s0:|enp4s0:" | wc -l)" -eq 2 ];then  
    # minicab
        sed -i -e 's/^Name=.*/Name=enp1s0/g' $USRCFG/LAN1.network 
        sed -i -e 's/^Name=.*/Name=enp4s0/g' $USRCFG/LAN2.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN3.network 
        sed -i -e 's/^Nams=.*/Name=null/g' $USRCFG/LAN4.network 
        sed -i -e 's/^Name=.*/Name=null/g' $USRCFG/LAN5.network 
    fi
    ln -sf $USRCFG/LAN1.network $NETCFG_DIR/LAN1.network
    ln -sf $USRCFG/LAN2.network $NETCFG_DIR/LAN2.network
    ln -sf $USRCFG/LAN3.network $NETCFG_DIR/LAN3.network
    ln -sf $USRCFG/LAN4.network $NETCFG_DIR/LAN4.network
    ln -sf $USRCFG/LAN5.network $NETCFG_DIR/LAN5.network

    $SYSCTL enable systemd-networkd.service || true
}

install_jkzuc(){
    CAP_ACCESS='CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_DAC_READ_SEARCH,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_SETGID,CAP_SETUID,CAP_SETPCAP,CAP_LINUX_IMMUTABLE,CAP_NET_BIND_SERVICE,CAP_NET_BROADCAST,CAP_NET_ADMIN,CAP_NET_RAW,CAP_IPC_LOCK,CAP_IPC_OWNER,CAP_SYS_MODULE,CAP_SYS_RAWIO,CAP_SYS_CHROOT,CAP_SYS_PTRACE,CAP_SYS_PACCT,CAP_SYS_ADMIN,CAP_SYS_BOOT,CAP_SYS_NICE,CAP_SYS_RESOURCE,CAP_SYS_TIME,CAP_SYS_TTY_CONFIG,CAP_MKNOD,CAP_LEASE=+eip'
    $SETCAP $CAP_ACCESS $JROOT/bin/shutils
    $SETCAP $CAP_ACCESS $JROOT/bin/zuctaskmod
    # $SETCAP $CAP_ACCESS /usr/bin/profinet
	# $SETCAP $CAP_ACCESS /usr/bin/ethernetip

    # 配置动态库的rpath，以避免动态库加载失败
    $PATCHELF --set-rpath $JROOT/bin $JROOT/bin/libzuctp.so*

    $SYSCTL enable jkzuc || true
    $SYSCTL enable jkbrd || true
    $SYSCTL enable jaka-ecat || true
    $SYSCTL enable nginx || true
}

config_system(){
    # 启动自动fsck修复
    # if [ -f /etc/default/grub ];then
    #     sed -i -e 's/^\(GRUB_CMDLINE_LINUX_DEFAULT=".*\)quiet"$ /\1quiet fsck.repair=yes"/g' /etc/default/grub
    #     update-grub
    # fi

    # 仅将notice等级以上的日志输出到daemon避免daemon过大占满磁盘
    if [ -f /etc/rsyslog.conf ];then
        sed -i -e 's/^daemon\..*/daemon.=notice      -\/var\/log\/daemon.log/g' /etc/rsyslog.conf
        rm -f /var/log/daemon.log*
    fi


    $SYSCTL disable apt-daily-upgrade || true
    $SYSCTL disable apt-daily || true
    
    # EtherCAT
    depmod
}
config_filesys(){
    # 准备配置文件结构
    install -d $USRCFG -m 777
    __intall_file $DEFCFG/usersettings.ini $USRCFG 666
    # 此处不覆盖密码文件，因为密码文件是用户自己修改的,与老版本不一样
    __intall_file $DEFCFG/jakar7_user.ini $USRCFG 666

    # 准备数据文件结构
    install -d $USRSCRIPT/track -m 777
    install -d $USRSCRIPT/program -m 777
    if [ ! -f $USRSCRIPT/program/movepoints.ini ];then
        echo "touch movepoints.ini ..."
        touch $USRSCRIPT/program/movepoints.ini
    fi
    chmod 777 $USRSCRIPT/program -R

    install_network
}

install_jkzuc
config_filesys
config_system
